package com.lagou.email.service.impl;

import com.lagou.email.api.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


@Service
@Slf4j
public class EmailServiceImpl implements EmailService {
  Log log = LogFactory.getLog(getClass());
  @Value("${spring.mail.username}")
  private String from;

  @Autowired
  private JavaMailSender mailSender;

  @Override
  public Boolean sendEmail(String email, String content) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo(email);
    message.setSubject("注册验证码");
    message.setText("您的验证码为：" + content + ",10分钟内有效");
    message.setFrom(from);
    try {
      mailSender.send(message);
    } catch (Exception e) {
      log.error("发送邮件失败，email：" + email + "content:" + content, e);
      return false;
    }
    return true;
  }
}
